<?php
/*
* This plugin uses parts of JCE extension by Ryan Demmer.
* @copyright	Copyright (C) 2005 - 2011 Ryan Demmer. All rights reserved.
* @copyright	Copyright (C) 2003 - 2011, CKSource - Frederico Knabben. All rights reserved.
* @license		GNU/GPL
* CKEditor extension is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
// no direct access
defined('_CKE_EXT') or die('Restricted access');
class JoomlalinksContent extends JObject {

	var $_option = 'com_content';
	/**
	* Constructor activating the default information of the class
	*
	* @access	protected
	*/
	function __construct($options = array()){
	}

	/**
	 * Returns a reference to a editor object
	 *
	 * This method must be invoked as:
	 * 		<pre>  $browser = &JContentEditor::getInstance();</pre>
	 *
	 * @access	public
	 * @return	JCE  The editor object.
	 * @since	1.5
	 */
	function &getInstance()
	{
		static $instance;

		if ( !is_object( $instance ) ){
			$instance = new JoomlalinksContent();
		}
		return $instance;
	}

	public function getOption()
	{
		return $this->_option;
	}

	public function getList()
	{
		$wf =& WFEditorPlugin::getInstance();

		if ($wf->checkAccess('content', 1)) {
			return '<li id="index.php?option=com_content"><div class="tree-row"><div class="tree-image"></div><span class="folder content nolink"><a href="javascript:;">' . JText::_('CONTENT') . '</a></span></div></li>';
		}
	}

	public function getLinks($args)
	{
		global $mainframe;

		$wf =& WFEditorPlugin::getInstance();

		require_once(JPATH_SITE .DS. 'components' .DS. 'com_content' .DS. 'helpers' .DS. 'route.php');

		$items 		= array();
		$view		= isset($args->view) ? $args->view : '';

		switch ($view) {
		default:
			$sections = self::_getSection();

			foreach ($sections as $section) {
					$id = ContentHelperRoute::getCategoryRoute($section->slug);

				$items[] = array(
					'id'		=>	$id,
					'name'		=>	$section->title,
					'class'		=>	'folder content'
				);
			}
			break;
		case 'section':
			$categories = WFLinkBrowser::getCategory('com_content', $args->id);
			foreach ($categories as $category) {
				$items[] = array(
					'id'		=>	ContentHelperRoute::getCategoryRoute($category->slug, $category->id),
					'name'		=>	$category->title . ' / ' . $category->alias,
					'class'		=>	'folder content'
				);
			}
			break;
		case 'category':
			$articles 	= self::_getArticles($args->id);

				$categories = WFLinkBrowser::getCategory('com_content', $args->id);

				foreach ($categories as $category) {

					$children 	= WFLinkBrowser::getCategory('com_content', $category->id);
					$id 		= $children ? 'index.php?option=com_content&view=section&id=' . $category->id : ContentHelperRoute::getCategoryRoute($category->slug, $category->id);

					$items[] = array(
						'id'		=>	$id,
						'name'		=>	$category->title . ' / ' . $category->alias,
						'class'		=>	'folder content'
					);
				}

			foreach ($articles as $article) {

					$id = ContentHelperRoute::getArticleRoute($article->slug, $article->catslug);

				$items[] = array(
					'id' 	=> $id,
					'name' 	=> $article->title . ' / ' . $article->alias,
					'class'	=> 'file'
				);
			}

			break;
		case 'uncategorized':
			$statics = self::_getUncategorized();
			foreach ($statics as $static) {
				$items[] = array(
					'id' 	=> ContentHelperRoute::getArticleRoute($static->id),
					'name' 	=> 	$static->title . ' / ' . $static->alias,
					'class'	=>	'file'
				);
			}
			break;
		}
		return $items;
	}

	private function _getSection()
	{
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser();
			return WFLinkBrowser::getCategory('com_content');
	}

	private function _getArticles($id)
	{
		$db			=& JFactory::getDBO();
		$user		=& JFactory::getUser();
		$wf 		=& WFEditorPlugin::getInstance();

		$query = 'SELECT a.id AS slug, b.id AS catslug, a.alias, a.title AS title';

		if ($wf->getSharedParam('article_alias', 1) == 1) {
			$query .= ', CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug';
		}

		$join 	= '';
		$where 	= '';
		$where	.= ' AND a.access IN ('.implode(',', $user->authorisedLevels()).')';

		$query .= ' FROM #__content AS a'
		. ' INNER JOIN #__categories AS b ON b.id = '.(int) $id
		. $join
		. ' WHERE a.catid = '.(int) $id
		. ' AND a.state = 1'
		. $where
		. ' ORDER BY a.title'
		;

		$db->setQuery($query, 0);
		return $db->loadObjectList();
	}

	private function _getUncategorized()
	{
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser();

		$where 	= '';
		$where	.= ' AND access IN ('.implode(',', $user->authorisedLevels()).')';

		$query = 'SELECT id, title, alias'
		. ' FROM #__content'
		. ' WHERE state = 1'
		. $where
		. ' AND catid = 0'
		. ' ORDER BY title'
		;
		$db->setQuery($query, 0);
		return $db->loadObjectList();
	}
}
?>